import request from '@/utils/request'
import { fileStore} from '@/stores/disk/file'
//文件分页接口
export function getPaging(currentPage,pageSize,index) {
    const data = {
        pageNum: currentPage,
        pageSize: pageSize,
        index: index
    }
    return request({
        url: "/api/file/paging",
        method: "get",
        params: data
    })
}
//文件大小小于20mb上传接口
export function uploadSmallFile(data,file) {
    const store= fileStore();
    return request({
        url: "/api/file/uploadSmall",
        method: "post",
        data: data,
        headers: {
            'Content-Type':'multipart/form-data'
        },
        onUploadProgress: (e) => {
            const percent = Math.round((e.loaded * 100) / e.total);
            store.updateUploadProgress(file.uid, percent)
        }
    })
}

//单个分片上传接口
export function uploadChunk(data,file) {
    const store= fileStore();
    return request({
        url: "/api/file/upload",
        method: "post",
        data: data,
        headers: {
            'Content-Type':'multipart/form-data'
        },
        onUploadProgress: (e) => {
            // 1. 将 chunkIndex 和 totalChunks 转为数字（避免字符串计算错误）
            const chunkIndex = Number(data.get('chunkIndex')); 
            const totalChunks = Number(data.get('totalChunks'));
            
            // 2. 计算每个分片的进度占比（如总分片数 5，每个分片占 20%）
            const chunkPercent = 100 / totalChunks; 
            
            // 3. 当前分片的上传进度（0-100%）
            const currentChunkProgress = Math.round((e.loaded / e.total) * 100);
            
            // 4. 总进度 = 已上传分片数 * 每个分片占比 + 当前分片进度 * 每个分片占比
            const totalPercent = (chunkIndex * chunkPercent) + (currentChunkProgress * chunkPercent / 100);
            
            // 5. 限制总进度在 0-100 之间（避免计算误差）
            const clampedPercent = Math.min(100, Math.max(0, totalPercent));
             // 关键修改：四舍五入到整数
            const formattedPercent = Math.round(clampedPercent); 
            store.updateUploadProgress(file.uid, formattedPercent)
        }
    })
}

//合并分片接口
export function mergeChunks(data,file) {
    const store= fileStore();
    return request({
        url: "/api/file/merge",
        method: "post",
        data: data,
    }).then((res) => {
        store.updateUploadProgress(file.uid, 100)
        return res;
    })
}

//删除文件到回收站接口
export function deleteFileToRecycle(data) {
    return request({
        url: "/api/file/update",
        method: "post",
        data: data
    })
}
//彻底删除文件接口
export function deleteFilesFinish(data) {
    return request({
        url: "/api/file/delete",
        method: "post",
        data: data
    })
}

//创建文件夹目录接口
export function createFolder(data) {
    return request({
        url: "/api/file/ensureDirectory",
        method: "post",
        data: data
    })
}
//文件夹分片上传接口
export function uploadFolderChunk(data) {
    return request({
        url: "/api/file/upload",
        method: "post",
        data: data,
        headers: {
            'Content-Type':'multipart/form-data'
        },
    })
}
//合并文件夹分片接口
export function mergeFolderChunks(data) {
    return request({
        url: "/api/file/mergeFolder",
        method: "post", 
        data: data,
        headers: { 'Content-Type': 'multipart/form-data' }     
    })
}

//预览文件接口
export function previewFile(data) {
    return request({
        url: "/api/file/preview",
        method: "post",
        data: data,
    })  
}

//点击文件夹接口
export function clickFolder(data) {
    return request({
        url: "/api/file/listObjects",
        method: "get",
        params: data,
    })  
}


//下载文件接口
export function downloadFile(data) {
    return request({
        url: "/api/file/download",
        method: "post",
        data: data,
        responseType: 'blob'
    })  
}

//新建文件夹
export function createNewFolder(data) {
    return request({
        url: "/api/file/createFolder",
        method: "get",
        params: data,
    })  
}

//搜索文件接口
export function searchFile(data) {
    return request({
        url: "/api/file/search",
        method: "get",
        params: data,
    })      
}

//恢复回收站文件接口
export function recoveryRecycleFile(data) {
    return request({
        url: "/api/file/recover",
        method: "put",
        data: data,
    })  
}


//重命名文件接口
export function renameFile(data) {
    return request({
        url: "/api/file/rename",
        method: "put",
        data: data,
    })  
}